package com.example.tellwin.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.common.Common;
import com.example.tellwin.mine.bean.OrderBean;
import com.example.tellwin.utils.CommonUtils;
import com.example.tellwin.utils.GlideUtils;
import com.example.tellwin.view.ContentTextView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author : LinZePeng
 * @date : 2020/11/23
 * @Describe ：
 **/
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {

    List<OrderBean> orderBeans = new ArrayList<>();
    Context context;

    public void setOrderBeans(List<OrderBean> orderBeans) {
        this.orderBeans = orderBeans;
        notifyDataSetChanged();
    }

    public void addOrderBeans(List<OrderBean> orderBeans) {
        this.orderBeans.addAll(orderBeans);
        notifyDataSetChanged();
    }

    public void clearData(){
        orderBeans.clear();
        notifyDataSetChanged();
    }

    OrderItemClickListener orderItemClickListener;

    public void setOrderItemClickListener(OrderItemClickListener orderItemClickListener) {
        this.orderItemClickListener = orderItemClickListener;
    }

    @NonNull
    @Override
    public OrderListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        if (MainApplication.Identity.equals(Common.ANSWER)) {
            return new OrderListViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_answer, parent, false));
        } else {
            return new OrderListViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_normal, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull OrderListViewHolder holder, int position) {
        OrderBean orderBean = orderBeans.get(position);
        holder.orderNumberTv.setTvStr(CommonUtils.getStr(orderBean.getOrderId()));
        if (MainApplication.Identity.equals(Common.ANSWER)) {
            GlideUtils.loadHeadImage(context, orderBean.getHeadPic(), holder.studentHeadIv);
            holder.questionClassTv.setText(orderBean.getGradeName() + " " + orderBean.getSubjectName());
            holder.questionTimeTv.setTvStr(orderBean.getCreateTime());
            holder.questionTitleTv.setText(CommonUtils.getStr(orderBean.getAskTitle()));
            holder.answerTimeTv.setTvStr(CommonUtils.getStr(orderBean.getAnswerDuration()));
            holder.studentNameTv.setText(orderBean.getRealName());
            holder.orderMoneyTv.setText("￥" + orderBean.getOrderPrice());
            if (orderBean.getOrderStatus() == 0 || orderBean.getOrderStatus() == 5) {
                holder.orderStateTv.setText("未接单");
                holder.orderMoneyRl.setVisibility(View.GONE);
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_d94850));
                holder.answerTimeTv.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 1) {
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.orderStateTv.setText("已完成");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_009EE0));
                holder.answerTimeTv.setVisibility(View.VISIBLE);
                holder.urgePayTv.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 2) {
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.orderStateTv.setText("已付款");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_FFB85C));
                holder.answerTimeTv.setVisibility(View.VISIBLE);
                holder.urgePayTv.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 3) {
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.orderStateTv.setText("已评价");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.theme_color));
                holder.answerTimeTv.setVisibility(View.VISIBLE);
                holder.urgePayTv.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 4) {
                holder.orderStateTv.setText("已取消");
                holder.orderMoneyRl.setVisibility(View.GONE);
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_d94850));
                holder.teacherContentRl.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 100) {
                holder.orderStateTv.setText("不满意");
                holder.orderMoneyRl.setVisibility(View.GONE);
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_d94850));
                holder.teacherContentRl.setVisibility(View.GONE);
            }
        } else {
            GlideUtils.loadHeadImage(context, orderBean.getHeadPic(), holder.teacherHeadIv);
            holder.teacherNameTv.setText(CommonUtils.getStr(orderBean.getRealName()));
            holder.answerTimeTv.setTvStr(CommonUtils.getStr(orderBean.getAnswerTime()));
            holder.teacherContentTv.setText(context.getResources().getString(R.string.teacher_content, CommonUtils.getStr(orderBean.getAnswerCount()), CommonUtils.getStr(orderBean.getSatisfiedCount())));
            holder.teacherIntroduceTv.setText(CommonUtils.getStr(orderBean.getGradeName()) + " " + CommonUtils.getStr(orderBean.getSubjectName()) + " " + CommonUtils.getAnswerType(orderBean.getAnswerType()));
            holder.questionTitleCtv.setTvStr(orderBean.getAskTitle());
            holder.questionTimeCtv.setTvStr(orderBean.getCreateTime());
            holder.orderMoneyTv.setText("￥ "+orderBean.getOrderPrice() );
            if (orderBean.getOrderStatus() == 4) {
                holder.orderStateTv.setText("已取消");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_9C9C9C));
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.payMoneyText.setVisibility(View.GONE);
                holder.orderMoneyTv.setVisibility(View.GONE);
                holder.normalContentRl.setVisibility(View.GONE);
                holder.questionLl.setVisibility(View.VISIBLE);
                holder.orderBtn.setVisibility(View.VISIBLE);
                holder.orderBtn.setText(R.string.order_again);
            } else if (orderBean.getOrderStatus() == 1) {
                holder.orderStateTv.setText("已完成");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_009EE0));
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.payMoneyText.setVisibility(View.VISIBLE);
                holder.orderMoneyTv.setVisibility(View.VISIBLE);
                holder.orderBtn.setVisibility(View.VISIBLE);
                holder.normalContentRl.setVisibility(View.VISIBLE);
                holder.questionLl.setVisibility(View.GONE);
                holder.orderBtn.setText(R.string.immediate_payment_);
                holder.payMoneyText.setText(R.string.fee_paid);
            } else if (orderBean.getOrderStatus() == 2) {
                holder.orderStateTv.setText("已付款");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_FFB85C));
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.payMoneyText.setVisibility(View.VISIBLE);
                holder.orderMoneyTv.setVisibility(View.VISIBLE);
                holder.orderBtn.setVisibility(View.VISIBLE);
                holder.normalContentRl.setVisibility(View.VISIBLE);
                holder.questionLl.setVisibility(View.GONE);
                holder.orderBtn.setText(R.string.immediate_evaluate);
                holder.payMoneyText.setText(R.string.fee_paided);

            } else if (orderBean.getOrderStatus() == 3) {
                holder.orderStateTv.setText("已评价");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_9c9c9c));
                holder.orderMoneyRl.setVisibility(View.VISIBLE);
                holder.payMoneyText.setVisibility(View.VISIBLE);
                holder.orderMoneyTv.setVisibility(View.VISIBLE);
                holder.normalContentRl.setVisibility(View.VISIBLE);
                holder.questionLl.setVisibility(View.GONE);
                holder.orderBtn.setVisibility(View.GONE);

            } else if (orderBean.getOrderStatus() == 0 || orderBean.getOrderStatus() == 5) {//待接单
                holder.orderStateTv.setText("等待接单中");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_d94850));
                holder.normalContentRl.setVisibility(View.GONE);
                holder.questionLl.setVisibility(View.VISIBLE);
                holder.orderMoneyRl.setVisibility(View.GONE);
            } else if (orderBean.getOrderStatus() == 100) {
                holder.orderStateTv.setText("不满意");
                holder.orderStateTv.setTextColor(context.getResources().getColor(R.color.color_d94850));
                holder.normalContentRl.setVisibility(View.GONE);
                holder.orderMoneyRl.setVisibility(View.GONE);
            }
        }
        holder.itemOrderView.setOnClickListener(v -> {
            if (orderItemClickListener != null) {
                orderItemClickListener.onOrderItemClick(position, orderBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return orderBeans.size();
    }

    private void initView() {
    }

    class OrderListViewHolder extends RecyclerView.ViewHolder {

        private ContentTextView orderNumberTv;
        private TextView orderStateTv;
        private ImageView studentHeadIv;
        private TextView studentNameTv;
        private TextView questionTitleTv;
        private TextView questionClassTv;
        private ContentTextView questionTimeTv;
        private ContentTextView answerTimeTv;
        private RelativeLayout orderMoneyRl;
        private TextView orderMoneyTv;
        private TextView shareFriendTv;
        private TextView urgePayTv;
        private LinearLayout itemOrderView;
        private RelativeLayout teacherContentRl;


        private ImageView teacherHeadIv;
        private TextView teacherNameTv;
        private TextView teacherContentTv;
        private TextView teacherIntroduceTv;
        private TextView orderBtn;
        private TextView payMoneyText;
        private RelativeLayout normalContentRl;
        private ContentTextView questionTitleCtv;
        private ContentTextView questionTimeCtv;
        private LinearLayout questionLl;

        public OrderListViewHolder(@NonNull View itemView) {
            super(itemView);
            itemOrderView = itemView.findViewById(R.id.item_order_view);
            orderNumberTv = (ContentTextView) itemView.findViewById(R.id.order_number_tv);
            orderStateTv = (TextView) itemView.findViewById(R.id.order_state_tv);
            studentHeadIv = (ImageView) itemView.findViewById(R.id.student_head_iv);
            studentNameTv = itemView.findViewById(R.id.student_name_tv);
            questionTitleTv = (TextView) itemView.findViewById(R.id.question_title_tv);
            questionClassTv = (TextView) itemView.findViewById(R.id.question_class_tv);
            questionTimeTv = (ContentTextView) itemView.findViewById(R.id.question_time_tv);
            answerTimeTv = (ContentTextView) itemView.findViewById(R.id.answer_time_tv);
            orderMoneyRl = (RelativeLayout) itemView.findViewById(R.id.order_money_rl);
            orderMoneyTv = (TextView) itemView.findViewById(R.id.order_money_tv);
            shareFriendTv = (TextView) itemView.findViewById(R.id.share_friend_tv);
            urgePayTv = (TextView) itemView.findViewById(R.id.urge_pay_tv);
            teacherContentRl = itemView.findViewById(R.id.item_order_teacher_content_rl);


            teacherHeadIv = (ImageView) itemView.findViewById(R.id.teacher_head_iv);
            teacherNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            teacherContentTv = (TextView) itemView.findViewById(R.id.teacher_content_tv);
            teacherIntroduceTv = (TextView) itemView.findViewById(R.id.teacher_introduce_tv);
            orderBtn = (TextView) itemView.findViewById(R.id.order_btn);
            payMoneyText = itemView.findViewById(R.id.pay_money_text);
            normalContentRl = itemView.findViewById(R.id.item_order_normal_conent_rl);
            questionTitleCtv = itemView.findViewById(R.id.question_title_ctv);
            questionTimeCtv = itemView.findViewById(R.id.question_time_ctv);
            questionLl = itemView.findViewById(R.id.question_ll);
        }
    }

    public interface OrderItemClickListener {
        void onOrderItemClick(int position, OrderBean orderBean);
    }
}
